package net.schmurgon.gwt.scaffold.generated;

import com.google.gwt.app.place.RecordDetailsView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import net.schmurgon.gwt.request.RunnerRecord;
import net.schmurgon.gwt.shared.Gender;

/**
 * Details view for Runner records.
 */
public class RunnerDetailsView extends Composite implements RecordDetailsView<RunnerRecord> {
	interface Binder extends UiBinder<HTMLPanel, RunnerDetailsView> {}

	private static final Binder BINDER = GWT.create(Binder.class);

	RunnerRecord record;
	
	@UiField SpanElement companyName;
	@UiField SpanElement emailAddress;
	@UiField SpanElement firstName;
	@UiField SpanElement gender;
	@UiField SpanElement lastName;	
	@UiField SpanElement displayRenderer;
	@UiField HasClickHandlers edit;
	@UiField HasClickHandlers delete;

	private Delegate delegate;

	public RunnerDetailsView() {
		initWidget(BINDER.createAndBindUi(this));
	}

  	public Widget asWidget() {
		return this;
	}
  
	public boolean confirm(String msg) {
		return Window.confirm(msg);
	}

	public RunnerRecord getValue() {
		return record;
	}

	@UiHandler("delete")
	public void onDeleteClicked(ClickEvent e) {
		delegate.deleteClicked();
	}

	@UiHandler("edit")
	public void onEditClicked(ClickEvent e) {
		delegate.editClicked();
	}

	public void setDelegate(Delegate delegate) {
		this.delegate = delegate;
	}

	public void setValue(RunnerRecord record) {
		this.record = record;
	    companyName.setInnerText(String.valueOf(record.getCompanyName()));
	    emailAddress.setInnerText(String.valueOf(record.getEmailAddress()));
	    firstName.setInnerText(String.valueOf(record.getFirstName()));
	    gender.setInnerText(String.valueOf(record.getGender()));
	    lastName.setInnerText(String.valueOf(record.getLastName()));

		displayRenderer.setInnerText(net.schmurgon.gwt.scaffold.generated.RunnerRecordRenderer.instance().render(record));
	}
}
